package com.dvt.faceScan;

//  查询条件 方法


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dvt.faceScan.httpUntil.XmlRpcUtils;
import com.dvt.faceScan.widget.Config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class employeeActivity extends Activity implements View.OnClickListener {
    private TextView tv1;//item.xml里的TextView：Textviewname
    private TextView tv2;//item.xml里的TextView：Textviewage
    private TextView tv3;//item.xml里的TextView：Textviewage
    private TextView tv4;//item.xml里的TextView：Textviewage
    private ListView lv;//activity_main.xml里的ListView
    private BaseAdapter adapter;//要实现的类
    private ArrayList<employeeModel> gzList = new ArrayList<employeeModel>();//实体类
    private String menu_flag = "";
    private String empName = "";
    private ImageView Iv1;
    private Button btn_search;
    private EditText et_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_employee);

        lv = (ListView) findViewById(R.id.listView2);
        Iv1 = (ImageView) findViewById(R.id.Iv_new);
        btn_search = (Button) findViewById(R.id.Btn_serach);
        et_search = (EditText) findViewById(R.id.Et_serach);
        btn_search.setOnClickListener(this);

        Iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(employeeActivity.this, employeeDetailActivity.class);
                intent.putExtra("flag", "2");
                startActivityForResult(intent, 103);
            }
        });

        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return gzList.size();//数目
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = employeeActivity.this.getLayoutInflater();
                View view;

                if (convertView == null) {
                    //因为getView()返回的对象，adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.item, null);
                } else {
                    view = convertView;
                    Log.i("info", "有缓存，不需要重新生成" + position);
                }
                tv1 = (TextView) view.findViewById(R.id.Textviewdate);//找到Textviewname
                tv1.setText(gzList.get(position).getName());//设置参数

                tv4 = (TextView) view.findViewById(R.id.Textviewinfo);//找到Textviewage
                tv4.setText(gzList.get(position).getInfo());//设置参数

                tv3 = (TextView) view.findViewById(R.id.Textviewgo);//找到Textviewage
                tv3.setText(">");//设置参数

                tv2 = (TextView) view.findViewById(R.id.Textviewcompany);//找到Textviewage
                tv2.setText(gzList.get(position).getCompany());//设置参数
                return view;
            }

            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;
            }

            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }
        };

        lv.setAdapter(adapter);
        //获取当前ListView点击的行数，并且得到该数据
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                employeeModel employee = new employeeModel();
                tv1 = (TextView) view.findViewById(R.id.Textviewdate);//找到Textviewname
                employee.setName(tv1.getText().toString());
                tv2 = (TextView) view.findViewById(R.id.Textviewcompany);//找到Textviewname
                employee.setCompany(tv2.getText().toString());
                tv4 = (TextView) view.findViewById(R.id.Textviewinfo);//找到Textviewname
                employee.setInfo(tv4.getText().toString());

                Intent intent = new Intent(employeeActivity.this, employeeDetailActivity.class);
                intent.putExtra("name", employee.getName());
                intent.putExtra("company", employee.getCompany());
                intent.putExtra("id", employee.getInfo());
                intent.putExtra("flag", "1");
                startActivity(intent);
            }
        });
        empName = "";
        requestEmployeeList();  //调接口
    }

    //调员工接口
    private void requestEmployeeList() {
        Callable<String> c = new Callable<String>() {
            @Override
            public String call() {
                int uid = 1;
                String db = Config.DB;
                String uname = Config.account;
                String pwd = Config.password;
                String model = Config.hr_employee;
                String odooUrl = Config.baseURL;
                String url = String.format("%s/xmlrpc/2/object", odooUrl);
                int action_id = 121;

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("action", action_id);
                Map<String, Object> context = new HashMap<String, Object>();
                context.put("lang", "zh_CN");
                context.put("tz", "Asia/Hong_Kong");
                context.put("uid", uid);
                context.put("params", params);

                List domain = asList(asList("name", "ilike", empName));

                Map<String, Object> options = new HashMap<String, Object>();
                options.put("context", context);
                options.put("domain", domain);
                options.put("fields", asList("name", "work_phone", "hired_company_id", "id"));
                options.put("limit", 80);
                Object requestObj = XmlRpcUtils.getXMLRPC(url, "execute_kw", asList(db, uid, pwd, model, "search_read", new ArrayList(),
                        options
                ));
                List r = asList((Object[]) requestObj);
                Log.d("wumh_debug", "报文：" + r);
                for (Object object : r) {
                    Map<String, Object> a = (HashMap) object;
                    employeeModel employee = new employeeModel();
                    String astr = String.valueOf(a.get("name"));
                    if (!"false".equals(String.valueOf(a.get("hired_company_id")))) {
                        Object[] o = (Object[]) a.get("hired_company_id");
                        List<Object> ol = asList(o);
                        for (Object _o : ol) {
                            String str = String.valueOf(_o);
                            Log.d("1", str);
                            employee.setCompany(str);
                        }
                    }
                    String iphone = String.valueOf(a.get("work_phone"));
                    String id = String.valueOf(a.get("id"));

                    if (astr.equals("false")) {
                        astr = "";
                    } else {
                        employee.setName(astr);
                    }
                    if (iphone.equals("false")) {
                        iphone = "";
                    } else {
                        employee.setIphone(iphone);
                    }

                    if (id.equals("false")) {
                        id = "";
                    } else {
                        employee.setInfo(id);
                    }

                    Set<String> akeySet = a.keySet();
                    for (String key : akeySet) {
                        Log.d("mmmmmmmm", key + "-->" + a.get(key));
                    }

                    gzList.add(employee);
                }
                Log.d("111111", "11111");
                return "";
            }
        };

        ExecutorService pool = Executors.newSingleThreadExecutor();
        Future<String> future = pool.submit(c);
        try {
            String result = future.get(5000, TimeUnit.SECONDS);
            Log.d("vvvvvv", result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private List asList(Object... a) {
        return Arrays.asList(a);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 103 && data != null) {
            requestEmployeeList();
        }
    }

    @Override
    public void onClick(View v) {
        if (btn_search == v) {
            empName = et_search.getText().toString();
            if(empName.length() == 0){
                empName = "";
            }
            gzList = new ArrayList<employeeModel>();//实体类
            requestEmployeeList();
            adapter.notifyDataSetChanged();
            lv.setAdapter(adapter);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        gzList = new ArrayList<employeeModel>();//实体类
        requestEmployeeList();
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);
    }
}
